package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.pojo.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class MenuServiceImpl implements MenuService {
   /* @Override
    public void add(Menu menu) {

    }*/

    @Autowired
    MenuDao menuDao;

    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        log.debug("service:" + queryPageBean);
        //1. 开始分页
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //2. 条件查询
        Page<CheckItem> page = menuDao.findByCondition(queryPageBean.getQueryString());
        log.debug("page{"+page+"}");
        //封装PageResult
        return new PageResult(page.getTotal(), page);
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    @Override
    public void add(Menu menu) {
        log.debug("service:{"+menu+"}");
        menuDao.add(menu);
    }

    @Override
    public void delById(Integer id) {
        long count = menuDao.findCountById(id);
        if(count > 0) {
            //说明被关联
            throw new RuntimeException("该检查项被检查组关联，不能删除！！");
        }else {
            menuDao.delById(id);
        }
    }

    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    @Override
    public List<Map<String, Object>> findMenuByUsername(String username) {
        //结果集合
        List<Map<String, Object>> resultMap= new ArrayList<>();
        //业务分析：  需要拿到所有父菜单和子菜单
        //根据用户名拿到所有菜单
        //根据level分出   父菜单  子菜单   pojo没有level 改用父菜单id
        //1.根据用户名查询用户信息
        //SysUser sysUser = menuDao.findUserByUsername(username);
        //定义父菜单集合 和 子菜单集合
         //得到用户信息
        SysUser user = menuDao.findByUsername(username);
        List<Menu> parentList=new ArrayList<>();
        List<Menu> childrenList=new ArrayList<>();
        //遍历得到所有菜单
        for(Role role:user.getRoles()){
            for (Menu menu1 : role.getMenus()) {
                if(menu1.getParentMenuId()==null){
                    parentList.add(menu1);
                }else {
                    childrenList.add(menu1);
                }
            }
        }

        //遍历把所有菜单 将子菜单添加到对应的父菜单
        for (Menu menu2 :parentList) {
            for (Menu menu3 : childrenList) {
                if(menu3.getParentMenuId()==menu2.getId()){
                    menu2.getChildren().add(menu3);
                }
            }
        }
        //封装返回值对象
        for(Menu parent:parentList) {//遍历父菜单
            Map<String, Object> map1 = new HashMap<>();
            map1.put("path", parent.getPath());
            map1.put("name", parent.getName());
            map1.put("icon", parent.getIcon());
            List<Map<String, Object>> List = new ArrayList<>();
            for (Menu child : parent.getChildren()) {//遍历子菜单
                Map<String,Object> map2=new HashMap<>();
                map2.put("path",child.getPath() );
                map2.put("name",child.getName() );
                map2.put("linkUrl",child.getLinkUrl() );
                List.add(map2);
            }
            map1.put("children",List );
            resultMap.add(map1);
        }
        log.debug("该用户的所有菜单"+resultMap.toString());
        return resultMap;
    }

}
